<template>
  <div v-loading="loading" class="printer-list-main">
    <!-- filter start -->
    <el-form ref="filterForm" :inline="true" :model="filterForm" label-width="120px" size="small">
      <el-form-item
        prop="name"
        label="打印机名称："
      >
        <el-input
          v-model="filterForm.name"
          placeholder="请输入"
          clearable
          class="app-w-350"
          @keyup.enter.native="submitFilterForm"
        />
      </el-form-item>
      <el-form-item
        prop="shopId"
        label="店铺："
      >
        <el-select v-model="filterForm.shopId" filterable clearable placeholder="可通过名称搜索" class="app-w-350">
          <el-option label="全部店铺" value />
          <el-option
            v-for="item in shopOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <div>
        <el-form-item label=" ">
          <el-button type="primary" round @click="submitFilterForm">查询打印机</el-button>
          <el-button round @click="resetFilterForm">重置条件</el-button>
        </el-form-item>
      </div>
    </el-form>

    <!--  列表  -->
    <div v-if="!loading&&list.length===0" class="empty-tips">
      查询暂无数据
    </div>
    <el-row v-else :gutter="16" type="flex">
      <el-col v-for="item in list" :key="'printer'+item.id" :md="12" :lg="8" :xl="6">
        <el-card :body-style="{ padding: '20px 15px'}">
          <svg-icon v-if="item.status!==1" icon-class="s-stop-using" class-name="s-stop-using" />
          <div class="relative">
            <div class="name app-ellipsis-l1">
              {{ item.name }}
            </div>
            <div class="more-info">
              <div class="app-ellipsis-l1">品牌：{{ brandMap[item.brandType] || '-' }}</div>
              <div class="app-ellipsis-l1">店铺：{{ item.shopName || '-' }}</div>
              <div class="app-ellipsis-l1">SN号：{{ item.snCode || '-' }}</div>
            </div>
            <div class="status-info">
              <span v-if="item.status===1" class="app-color-primary">启用中</span>
              <span v-else>已停用</span>
            </div>
          </div>
          <div class="handle-group">
            <el-button type="text" size="mini" icon="el-icon-edit" circle @click.native="handleEdit(item)"> 编辑
            </el-button>
            <el-button type="text" size="mini" icon="el-icon-delete" class="app-color-danger" @click.native="handleDel(item)">删除
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!--分页-->
    <PaginationDirection
      v-if="paginationConfig.total!==0"
      :config.sync="paginationConfig"
      @sizesChange="fetchData"
      @pageChange="fetchData"
    />

    <!--组件-->
    <MchPrinterFormDialog ref="mchPrinterFormDialog" />

  </div>
</template>

<script>
import MchPrinterFormDialog from './MchPrinterFormDialog'
import { printerList, delPrinter } from '@/api/mch/equipment'
import { allShopList } from '@/api/mch/shop'

export default {
  components: {
    MchPrinterFormDialog
  },
  data() {
    return {
      // 配置
      // 分页配置
      paginationConfig: {
        direction: 'center', // 分页位置
        layout: 'total, sizes, prev, pager, next, jumper', // 分页类型
        pageSizes: [10, 20], // 分页pages
        total: 0,
        page: 1, // 页数-默认1
        pageSize: 10 // 每页条数-默认10
      },
      brandMap: {
        1001: '飞鹅',
        2001: '易联云',
        3001: '博实结'
      },
      // 状态
      loading: false,
      // 数据
      shopOptions: [],
      filterForm: {
        businessId: null,
        name: '',
        shopId: null,
        pageNum: 1,
        pageSize: 10
      },
      list: []
    }
  },
  computed: {},
  watch: {},
  beforeCreate() {
  },
  created() {
    // 定义全局刷新方法
    this.$root.$on('refreshMchEquipmentPrinterList', () => this.fetchData())
  },
  beforeMount() {
  },
  mounted() {
    this.getAllShopOptions()
    this.fetchData()
  },
  beforeUpdate() {
  },
  update() {
  },
  activated() {
  },
  deactivated() {
  },
  beforeDestroy() {
    this.$root.$off('refreshMchEquipmentPrinterList')
  },
  destroyed() {
  },
  methods: {
    /**
     * 数据组
     */
    // 获取列表
    fetchData(
      page = this.paginationConfig.page,
      pageSize = this.paginationConfig.pageSize
    ) {
      // 请求参数
      const params = Object.assign({}, this.filterForm)
      params.pageNum = page
      params.pageSize = pageSize
      params.businessId = this.$route.query.mchId
      // 清除多余字段
      for (const key in params) {
        if (this.$isNull(params[key])) delete params[key]
      }
      // 请求
      this.loading = true
      printerList({ data: params })
        .then(res => {
          this.list = res.data.list || []
          this.paginationConfig.total = res.data.total
          this.paginationConfig.page = res.data.pageNum
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 获取所有门店
    getAllShopOptions() {
      const params = {
        businessId: this.$route.query.mchId
      }
      this.loading = true
      allShopList({ params }).then(res => {
        this.shopOptions = res.data || []
      }).finally(() => {
        this.loading = false
      })
    },
    /**
     * 按钮组
     */
    // 查询
    submitFilterForm() {
      this.fetchData(1)
    },

    // 重置
    resetFilterForm() {
      this.$refs.filterForm.resetFields()
    },

    // 编辑
    handleEdit(row) {
      this.$refs.mchPrinterFormDialog.open({ mode: 'edit', row }).then(() => {
        this.fetchData()
      })
    },

    // 删除
    handleDel(row) {
      this.$confirm(`确定删除打印机 ：${row.name}?`, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true,
        roundButton: true
      }).then(() => {
        this.loading = true
        const params = {
          businessId: row.businessId,
          printerId: row.id
        }
        delPrinter({ params }).then(() => {
          this.fetchData()
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
        }).finally(() => {
          this.loading = false
        })
      })
    }
    /**
     * 状态组
     */
    /**
     * 方法组
     */
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.printer-list-main {
  padding-left: 20px;
  /*reset*/
  .el-row {
    flex-wrap: wrap;

    .el-col {
      margin-bottom: 16px;
      height: 100%;
    }
  }

  /*卡片*/
  .el-card {
    position: relative;
    border-radius: 20px;

    .handle-group {
      position: absolute;
      right: 10px;
      top: 5px;
      //display: none;
    }

    //:hover .handle-group {
    //  display: block;
    //}

    .s-stop-using{
      position: absolute;
      right: 20px;
      top: 30px;
      font-size: 100px;
    }

    /*title*/
    .title {
      height: 50px;
      line-height: 50px;
      font-size: 18px;
      font-weight: bold;
    }

    /*card*/
    .name {
      font-size: 18px;
      padding-top: 10px;
    }

    .more-info {
      font-size: 14px;
      color: #909399;

      div {
        margin-top: 10px;
      }
    }

    .status-info {
      margin-top: 10px;
      padding-top: 10px;
      border-top: 1px solid #f5f5f5;
      color: #909399;
    }
  }

  /*空提示*/
  .empty-tips {
    text-align: center;
    font-size: 12px;
    padding-top: 100px;
  }
}

</style>
